package com.jron.cqiu.kernel.implement.crossos.jna;

import jnacontrib.win32.Registry.REGISTRY_ROOT_KEY;
import jnacontrib.win32.Registry;
import com.jron.cqiu.kernel.regist.IRegist;

import java.util.TreeMap;
import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by Intellij IDEA.
 *
 * @Author: Yejw
 * @Date: 2008-6-16
 * @Time: 14:57:00
 */
public class JnaRegist implements IRegist {

    private static Log log = LogFactory.getLog(JnaRegist.class);



    //==================================================================================================================

    /**
     * Read a String value.
     *
     * @param rootKey    root key
     * @param subKeyName key name
     * @param name       value name
     * @return String or null
     */
    public String getStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) {
        try {
            return Registry.getStringValue(rootKey, subKeyName, name);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.debug(e.getMessage());
            return null;
        }
    }

    /**
     * Read an int value.
     *
     * @param rootKey    root key
     * @param subKeyName key name
     * @param name       value name
     * @return int or 0
     */
    public int getIntValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) {
        return Registry.getIntValue(rootKey, subKeyName, name);
    }

    /**
     * Delete a value.
     *
     * @param rootKey    root key
     * @param subKeyName key name
     * @param name       value name
     * @return true on success
     */
    public boolean deleteValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) {
        return Registry.deleteValue(rootKey, subKeyName, name);
    }

    /**
     * Writes a String value.
     *
     * @param rootKey    root key
     * @param subKeyName key name
     * @param name       value name
     * @param value      value
     * @return true on success
     */
    public boolean setStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name, String value) {
        try {
            return Registry.setStringValue(rootKey, subKeyName, name, value);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * Writes an int value.
     *
     * @param rootKey    root key
     * @param subKeyName key name
     * @param name       value name
     * @param value      value
     * @return true on success
     */
    public boolean setIntValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name, int value) {
        return Registry.setIntValue(rootKey, subKeyName, name, value);
    }

    /**
     * Check for existence of a value.
     *
     * @param rootKey    root key
     * @param subKeyName key name
     * @param name       value name
     * @return true if exists
     */
    public boolean valueExists(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) {
        return Registry.valueExists(rootKey, subKeyName, name);
    }

    /**
     * Create a new key.
     *
     * @param rootKey root key
     * @param parent  name of parent key
     * @param name    key name
     * @return true on success
     */
    public boolean createKey(REGISTRY_ROOT_KEY rootKey, String parent, String name) {
        return Registry.createKey(rootKey, parent, name);
    }

    /**
     * Delete a key.
     *
     * @param rootKey root key
     * @param parent  name of parent key
     * @param name    key name
     * @return true on success
     */
    public boolean deleteKey(REGISTRY_ROOT_KEY rootKey, String parent, String name) {
        return Registry.deleteKey(rootKey, parent, name);
    }

    /**
     * Get all sub keys of a key.
     *
     * @param rootKey root key
     * @param parent  key name
     * @return array with all sub key names
     */
    public String[] getSubKeys(REGISTRY_ROOT_KEY rootKey, String parent) {
        return Registry.getSubKeys(rootKey, parent);
    }

    /**
     * Get all values under a key.
     *
     * @param rootKey root key
     * @param key     jey name
     * @return TreeMap with name and value pairs on error
     */
    public TreeMap<String, Object> getValues(REGISTRY_ROOT_KEY rootKey, String key) {
        try {
            return Registry.getValues(rootKey, key);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
